package ahblue.cn.tanzi.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.SparseArray;

public class NetUtils {
	
	/**
	 * 判断网络是否连接
	 * @param context
	 * @return
	 */
	public static boolean isConnected(Context context) {
		boolean flag=false;
	     if (context != null) {  
	         ConnectivityManager mConnectivityManager = (ConnectivityManager) context
	                 .getSystemService(Context.CONNECTIVITY_SERVICE);
	         NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
	         if (mNetworkInfo != null) {  
	        	 flag=mNetworkInfo.isAvailable();  
	             return flag;  
	         }  
	     }
	     return flag;  
	 }

	/**
	 * 判断是否是wifi连接
	 * @param context
	 * @return
	 */
	public static boolean isWifi(Context context){
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		return wifi.isWifiEnabled();//返回true时表示存在，
	}
	/**
	 * 打开网络设置界面
	 */
	public static void openSetting(Activity activity)
	{
		Intent intent = new Intent("/");
		ComponentName cm = new ComponentName("com.android.settings",
				"com.android.settings.WirelessSettings");
		intent.setComponent(cm);
		intent.setAction("android.intent.action.VIEW");
		activity.startActivityForResult(intent, 0);
	}

	/**
	 * 网络类型
	 * wifi、edge、gprs、cdma 等
	 * @return
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
	public static String getNetworkType(Context context){
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(null==connectMgr){
			return "";
		}
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(null==info){
			return "";
		}
		if(info.getType() == ConnectivityManager.TYPE_WIFI){
			return "wifi";
		}
		if(info.getType() ==  ConnectivityManager.TYPE_MOBILE){
			int type=info.getSubtype();
			SparseArray sparseArray=new SparseArray();
			sparseArray.put(TelephonyManager.NETWORK_TYPE_UNKNOWN,"unknown");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_GPRS,"gprs");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_EDGE,"edge");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_UMTS,"umts");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_CDMA,"cdma");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_EVDO_0,"evdo");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_EVDO_A,"evdo");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_1xRTT,"1xrtt");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_HSDPA,"hsdpa");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_HSUPA,"hsupa");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_HSPA,"hspa");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_IDEN,"iden");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_EVDO_B,"evdo");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_LTE,"lte");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_EHRPD,"ehrpd");
			sparseArray.put(TelephonyManager.NETWORK_TYPE_HSPAP,"hspa+");
			sparseArray.put(16,"gsm");
			return null==sparseArray.get(type)?"":sparseArray.get(type).toString();
		}
		return "";
	}

	/**
	 * 网络连接类型，（ UNKNOWN, ETHERNET, WIFI, MOBILE;），值可能为0,1,2,3
	 * @param context
	 * @return
	 */
	public static int getNetType(Context context){
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(null==connectMgr){
			return 0;
		}
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(null==info){
			return 0;
		}
		if(info.getType() == ConnectivityManager.TYPE_ETHERNET){
			return 1;
		}
		if(info.getType() == ConnectivityManager.TYPE_WIFI){
			return 2;
		}
		if(info.getType() ==  ConnectivityManager.TYPE_MOBILE){
			return 3;
		}
		return 0;
	}
	/**
	 * 子网络连接类型。当ct字段为MOBILE时添加，为3G时值可为6,3,5,8,9,10,12,14,15之一，4G时为13
	 */
	public static int getMobileType(Context context){
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(null==connectMgr){
			return 0;
		}
		NetworkInfo info = connectMgr.getActiveNetworkInfo();
		if(null==info){
			return 0;
		}
		if(info.getType() ==  ConnectivityManager.TYPE_MOBILE){
			return  info.getSubtype();
		}
		return 0;
	}
}
